<template>
  <div>
    <el-form label-width="85px" label-position="left" class="message">
      <el-form-item label="管理单位：">
        <div>{{ $store.getters.managerName }}</div>
      </el-form-item>
      <el-form-item label="邮箱：">
        <div>{{ $store.getters.eMail }}</div>
      </el-form-item>
      <el-form-item label="手机：">
        <div>{{ $store.getters.phone }}</div>
      </el-form-item>
      <el-button type="primary" @click="showUpdate = true">修改资料</el-button>
      <el-button type="danger" @click="showPassword = true">修改密码</el-button>
    </el-form>

    <el-dialog title="更新资料" :visible.sync="showUpdate" :close-on-click-modal="false">
      <el-form label-width="80px" label-position="left" style="width: 600px; margin-left:50px;" @keyup.enter.native="updateAdminMessage">
        <el-form-item label="管理单位">
          <el-input v-model.trim="managerName" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model.trim="eMail" />
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model.trim="phone" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateAdminMessage">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="修改密码" :visible.sync="showPassword" :close-on-click-modal="false">
      <el-form ref="passwordForm" label-width="80px" :model="passwordForm" label-position="left" :rules="passwordRules" style="width: 600px; margin-left:50px;" @keyup.enter.native="updateAdminPassword('passwordForm')">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model.trim="passwordForm.oldPassword" type="password" show-password />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model.trim="passwordForm.newPassword" type="password" show-password />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input v-model.trim="passwordForm.checkPassword" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateAdminPassword('passwordForm')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>

</template>

<script>
import { updateAdminMessage, updateAdminPassword } from '@/api/user'
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.passwordForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      showUpdate: false,
      managerName: this.$store.getters.managerName,
      eMail: this.$store.getters.eMail,
      phone: this.$store.getters.phone,
      showPassword: false,
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        checkPassword: ''
      },
      passwordRules: {
        oldPassword: [
          {
            required: true, message: '请输入原密码', trigger: 'blur'
          }
        ],
        newPassword: [
          {
            required: true, message: '请输入新密码', trigger: 'blur'
          }
        ],
        checkPassword: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    updateAdminMessage() {
      updateAdminMessage({ managerName: this.managerName, eMail: this.eMail, phone: this.phone }).then(res => {
        this.$store.dispatch('user/getAdminUser')
        this.showUpdate = false
        // location.reload()
        this.$message({
          message: '信息更新成功!',
          type: 'success',
          duration: 5 * 1000
        })
      })
    },
    updateAdminPassword(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateAdminPassword(this.passwordForm).then(res => {
            this.$message({
              message: '密码修改成功!',
              type: 'success',
              duration: 5 * 1000
            })
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .message{
    .el-form-item{
      margin-bottom: 8px;
    }
  }
</style>
